package com.jds.test.bio.p11;

/**
 * https://www.cnblogs.com/silyvin/articles/12064484.html
 * https://www.cnblogs.com/silyvin/articles/12070767.html
 * Created by joyce on 2019/11/26.
 */


import java.net.InetSocketAddress;
import java.net.Socket;


public class Client {

    public static final int PORT = 12123;
    public static final int BUFFER_SIZE = 1024;

    public static void main(String []f) throws Exception {
        new Client().client();
    }

    //客户端代码
    public void client() throws Exception {
        final String s1 = "49.235.75.155";// tx
        final String s2 = "localhost";
        byte[] buffer;
        Socket s = new Socket();

        /**
         * 决定rst or fin
         */
        //   s.setSoLinger(true, 0);
        s.bind(new InetSocketAddress(8888));
        s.connect(new InetSocketAddress(s1, PORT));
        int i = s.getInputStream().read(buffer = new byte[BUFFER_SIZE]);
        System.out.println(new String(buffer, 0, i));

        /**
         * 程序自然结束，close，将会导致四次挥手失败
         */
        int flag = 22;
        if(flag == 0) {

        } else if(flag == 11) {
            s.close();
        } else if(flag == 12) {
            s.shutdownOutput();
        } else if(flag == 21) {
            s.close();
            Thread.sleep(10000);
        } else if(flag == 22) {
            s.shutdownOutput();
            Thread.sleep(10000);
        }

    }
}